<?php
/**
 * Created by PhpStorm.
 * User: Liuhongchuan
 * Date: 2018/4/28
 * Time: 14:58
 * 设备管理
 */
namespace app\operation\controller;

use app\common\controller\OperationBase;

class Equipment extends OperationBase {

	protected $client='';

	public function _initialize() {
		parent::_initialize();
		$this->client = new \app\api\controller\JpusHelper();
    }

    /**
     * 设备列表
     */
	public function index(){
        $auth_group = sp_get_auth_menu_3("20061000");
        $shops = db("shop")->where(array("parent_id" => array("gt", 0)))->order("parent_id desc, shop_name desc")->column("id, shop_name");
        $this->assign("shops", $shops);
        $this->assign("menus", $auth_group);
        echo $this->fetch();
	}

    /**
     * 设备列表数据
     *
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function ajaxIndex(){
        $mac_addr = input("mac_addr/s");
        $name = input("name/s");
        $shop_id = input("shop_id/d");
        $draw = input("draw",0) + 1;//请求时间

        $where = array();
        if (!empty($mac_addr)) {
            $where["e.mac_addr"] = array("like", "%{$mac_addr}%");
        }
        if (!empty($name)) {
            $where["e.name"] =  array("like", "%{$name}%");
        }
        if (!empty($shop_id)) {
            $where["e.shop_id"] = $shop_id;
        }

        $total = db("equipment")->alias("e")->where($where)->count(1);
        $list = db("equipment")->alias("e")
            ->field("e.*, s.shop_name")
            ->join("m_shop s", "s.id = e.shop_id", "left")
            ->where($where)
            ->order($this->get_order())
            ->limit($this->get_limit())
            ->select();
        $data = array('pageData' => $list, 'total' => $total, "draw" => $draw);
        $this->ajax($data);
    }

	//删除
	public function delete() {
        $id = input("id/d");
        if (empty($id)) {
            $this->error("参数错误");
        }
        $delete_res = db("equipment")->where(array("id" => $id))->delete();
        if (empty($delete_res)) {
            $this->ajax_error("失败");
        }
        $this->ajax_success("成功");
	}

    /**
     * 添加
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
	public function add() {
		if (request()->isPost()) {
		    $mac_addr = input("mac_addr/s","");
		    $name = input("name/s", "");
		    $desc = input("desc/s", "");
		    $type = input("type/d", 1);
		    $version = input("version/s", "");
		    $shop_id = input("shop_id/d", 0);
		    if (empty($mac_addr)) {
		        $this->ajax_error("设备地址不能为空");
            }
            if (empty($name)) {
                $this->ajax_error("设备名称不能为空");
            }
            if (empty($desc)) {
                $this->ajax_error("设备描述不能为空");
            }
            if (empty($shop_id)) {
                $this->ajax_error("所属店铺不能为空");
            }
            $shop = db("shop")->find($shop_id);
		    if (empty($shop)) {
		        $this->ajax_error("店铺不存在");
            }
		    $count = db("equipment")->where(array("mac_addr" => array("eq", $mac_addr)))->count(1);
		    if ($count > 0) {
			   	$this->ajax_error("mac_addr 已经存在");
		    }
            $insert_res = db("equipment")->insert(array(
                "mac_addr"  => $mac_addr,
                "name"      => $name,
                "descs"     => $desc,
                "shop_id"   => $shop_id,
                "type"      => $type,
                "version"   => $version
            ));
		    if (empty($insert_res)) {
		        $this->ajax_error("失败");
            }
			$this->ajax_success("成功");	
		} else {
            $shops = db("shop")->where(array("parent_id" => array("neq", 0)))->order("parent_id desc, shop_name desc")->select();
            $this->assign("shops", $shops);
			echo $this->fetch();	
		}
	}
	/**
	 * 设备重启
	 * */
	function restart(){
			$ids=input("ids");	
			$list=db("Equipment")->where(" id in ($ids)")->field("id,type,mac_addr")->select();
			$result1=null;//私教设备
			$result2=null;//人脸平板
			$result3=null;//tv盒子
			foreach ($list as $key => $value) {
				  if($value['type']==1){
				  		$result1[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }else if($value['type']==2){
				  		$result2[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }else if($value['type']==3){
				  		$result3[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }
			}
			//私教列表 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result1)){
					$this->client->send_notice($result1,"","",11,"app 重启命令");	
			}

			//人脸列表 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result2)){
					$this->client->send_face_notice($result2,"","",11,"app 重启命令");	
			}
			
			//tv盒子 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result3)){
					$this->client->tv($result3,"","",11,"app 重启命令");	
			}
			$this->ajax_success("成功");	
	}
	/**
	 * 数据库上传
	 * */
	function upload(){
			$ids=input("ids");	
			$list=db("Equipment")->where(" id in ($ids)")->field("id,type,mac_addr")->select();
			$result1=null;//私教设备
			$result2=null;//人脸平板
			$result3=null;//tv盒子
			foreach ($list as $key => $value) {
				  if($value['type']==1){
				  		$result1[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }else if($value['type']==2){
				  		$result2[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }else if($value['type']==3){
				  		$result3[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }
			}
			//私教列表 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result1)){
					$this->client->send_notice($result1,"","",12,"app 上传数据库命令");	
			}

			//人脸列表 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result2)){
					$this->client->send_face_notice($result2,"","",12,"app 上传数据库命令");	
			}
			
			//tv盒子 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result3)){
					$this->client->tv($result3,"","",12,"app 上传数据库命令");	
			}
			$this->ajax_success("成功");	
	}
	/**
	 * 更新提示
	 * */
	function tips(){
			$ids=input("ids");	
			$result1=null;//私教设备
			$result2=null;//人脸平板
			$result3=null;//tv盒子
			$list=db("Equipment")->where(" id in ($ids)")->field("id,type,mac_addr")->select();
			foreach ($list as $key => $value) {
				  if($value['type']==1){
				  		$result1[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }else if($value['type']==2){
				  		$result2[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }else if($value['type']==3){
				  		$result3[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }
			}
			//私教列表 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result1)){
					$this->client->send_notice($result1,"","",13,"app 提示更新下载命令");	
			}

			//人脸列表 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result2)){
					$this->client->send_face_notice($result2,"","",13,"app 提示更新下载命令");	
			}
			
			//tv盒子 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result3)){
					$this->client->tv($result3,"","",13,"app 提示更新下载命令");	
			}
			$this->ajax_success("成功");	
	}
	function send_test(){
			$ids=input("ids");	
			$result1=null;//私教设备
			$result2=null;//人脸平板
			$result3=null;//tv盒子
			$list=db("Equipment")->where(" id in ($ids)")->field("id,type,mac_addr")->select();
			foreach ($list as $key => $value) {
				  if($value['type']==1){
				  		$result1[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }else if($value['type']==2){
				  		$result2[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }else if($value['type']==3){
				  		$result3[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }
			}
			//私教列表 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result1)){
					$this->client->send_notice($result1,"","",99,"app 推送测试");	
			}

			//人脸列表 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result2)){
					$this->client->send_face_notice($result2,"","",99,"app 推送测试");	
			}
			
			//tv盒子 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result3)){
					$this->client->tv($result3,"","",99,"app 推送测试");	
			}
			$this->ajax_success("成功");	
	}
	/**
	 * 图片上传
	 * */
	function pic_reupload(){
		if(empty(input("ids"))){
			echo "ids 为空";
		}else{
			$this->assign("ids",input("ids"));
			echo $this->fetch();
		}
	}
	function pic_reupload_post(){
			$ids=input("ids");	
			$url=input("url");
			$list=db("Equipment")->where(" id in ($ids)")->field("id,type,mac_addr")->select();
			$result1=null;//私教设备
			$result2=null;//人脸平板
			$result3=null;//tv盒子
			foreach ($list as $key => $value) {
				  if($value['type']==1){
				  		$result1[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }else if($value['type']==2){
				  		$result2[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }else if($value['type']==3){
				  		$result3[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }
			}
			//私教列表 send_notice($alias=array(''),$v="1.0",$url="",$type=1,$title="app更新推送")
			if(!empty($result1)){
					$this->client->send_notice($result1,"",$url,20,"图片重传命令");	
			}

			//人脸列表 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result2)){
					$this->client->send_face_notice($result2,"",$url,20,"图片重传命令");	
			}
			
			//tv盒子 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result3)){
					$this->client->tv($result3,"",$url,20,"图片重传命令");	
			}
			$this->ajax_success("成功");	
	}

/**
	 * 图片上传
	 * */
	function set_score(){
		if(empty(input("ids"))){
			echo "ids 为空";
		}else{
			$this->assign("ids",input("ids"));
			echo $this->fetch();
		}
	}
	function set_score_post(){
			$ids=input("ids");	
			$url=input("url");
			$list=db("Equipment")->where(" id in ($ids)")->field("id,type,mac_addr")->select();
			$result1=null;//私教设备
			$result2=null;//人脸平板
			$result3=null;//tv盒子
			foreach ($list as $key => $value) {
				  if($value['type']==1){
				  		$result1[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }else if($value['type']==2){
				  		$result2[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }else if($value['type']==3){
				  		$result3[]=str_replace(array(":"), array(""),$value['mac_addr']);
				  }
			}
			//私教列表 send_notice($alias=array(''),$v="1.0",$url="",$type=1,$title="app更新推送")
			if(!empty($result1)){
					$this->client->send_notice($result1,"",$url,21,"设置比分阀值");	
			}

			//人脸列表 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result2)){
					$this->client->send_face_notice($result2,"",$url,21,"设置比分阀值");	
			}
			
			//tv盒子 send_notice($alias=array(''),$v="1.0",$url="",$type=1)
			if(!empty($result3)){
					$this->client->tv($result3,"",$url,21,"设置比分阀值");	
			}
			$this->ajax_success("成功");	
	}

	/*更新*/
	public function update(){
	    $version = input("version/s");
	    if (!empty($version)) {
			$ids = input("ids");
			$addrs = input("addrs/s");
			if (empty($ids)) {
			    $this->error("参数错误");
            }
            if (empty($addrs)) {
                $this->error("地址错误");
            }
			db("equipment")->where(array("id" => array("in", $ids)))->setField(array("version" => $version));
			$alias = db("equipment")->where(array("id" => array("in", $ids)))->column("mac_addr");

			$result = '';
			foreach ($alias as $key => $value) {
				$result[] = str_replace(array(":"), array(""), $value);
			}
			//私教打卡
			$this->client->send_notice($result, $value, $addrs);
			$this->ajax_success("成功");	
		} else {
			$ids = input("ids");
			$this->assign("ids",$ids);
			echo $this->fetch();	
		}
	}
}
